clear all
tempfile tempsave
set seed ${seed}


// --------
// ACS 2019
// --------
use "${rawdata}acs/usa_00005.dta" if birthyr>1973 & birthyr<1984, clear
keep perwt birthyr hispan race sex educd

*gender
gen gender_cat=(sex==2)
tab gender_cat, gen(gender_cat_)

*years of education
gen educd_cat=.
replace educd_cat=1 if educd<64
replace educd_cat=2 if educd>=64 & educd<101
replace educd_cat=3 if educd>=101 & educd!=.
tab educd_cat, gen(educresp_cat_)

*save
keep 	gender_cat_* ///
		educresp_cat_* ///
		perwt hispan race
foreach x of varlist _all{
	sum `x' [aw=perwt], meanonly
	local `x'_pop=r(mean)

	sum `x' [aw=perwt] if hispan==0 & race==1
	local `x'_white=r(mean)
}

// Total size of relevant population group (used for reweighting)
local total=r(sum_w)

// --------
// CPS 1994
// --------
use "${rawdata}cps/cps_1994.dta", clear
keep asecwt hispan race eldch yngch age sex educ hhincome

*kids in HH
recode eldch (99=.)
recode yngch (99=.)
gen byear_respondent=1994-age
gen byear_maxchild	=1994-eldch
gen byear_minchild	=1994-yngch

gen child= (byear_minchild>1973 & byear_minchild<1984) ///
		 | (byear_maxchild>1973 & byear_maxchild<1984) ///
		 if byear_maxchild!=.
keep if child==1

*education
gen educ_cat=.
replace educ_cat=1 if educ<74 & educ!=1
replace educ_cat=2 if educ>=74 & educ<101
replace educ_cat=3 if educ>=111 & educ!=.

*education mother
gen educmother_cat=educ_cat if sex==2
tab educmother_cat, gen(educmother_cat_)

*education father
gen educfather_cat=educ_cat if sex==1
tab educfather_cat, gen(educfather_cat_)

*age mother at birth
gen agemum_max=age-eldch if sex==2
replace agemum_max=. if agemum_max<10 | agemum_max>70
gen agemum_min=age-yngch if sex==2
replace agemum_min=. if agemum_min<10 | agemum_min>70

gen agemum = (agemum_min+agemum_max)/2
gen agemum_cat=1 if agemum<25
replace agemum_cat=2 if agemum>=25 & agemum!=.

tab agemum_cat, gen(agemum_cat_)

*income
gen hhincome_cat=.
replace hhincome_cat=1 if hhincome<50000
replace hhincome_cat=2 if hhincome>=50000 & hhincome<100000
replace hhincome_cat=3 if hhincome>=100000 & hhincome!=.

tab hhincome_cat, gen(hhincome_cat_)

keep 		educmother_cat_* ///
			educfather_cat_* ///
			agemum_cat_*  ///
			hhincome_cat_*  ///
			asecwt hispan race
foreach x of varlist _all{
	sum `x' [aw=asecwt], meanonly
	local `x'_pop=r(mean)

	sum `x' [aw=asecwt] if hispan==0 & race==100, meanonly
	local `x'_white=r(mean)
}

// ----------
// Add Health
// ----------
use "${data}data_estim.dta" if sample1==1, clear
keep aid w0 gender edumother edufather agemum inc_ln eduyears

*gender
tab gender, gen(gender_cat_)

*education mother
gen educmother_cat=.
replace educmother_cat=1 if edumother<13
replace educmother_cat=2 if edumother>=13 & edumother<16
replace educmother_cat=3 if edumother>=16 & edumother!=.
tab educmother_cat, gen(educmother_cat_)

*education father
gen educfather_cat=.
replace educfather_cat=1 if edufather<13
replace educfather_cat=2 if edufather>=13 & edufather<16
replace educfather_cat=3 if edufather>=16 & edufather!=.
tab educfather_cat, gen(educfather_cat_)

*age mother at birth
gen agemum_cat=.
replace agemum_cat=1 if agemum<25
replace agemum_cat=2 if agemum>=25 & agemum!=.
tab agemum_cat, gen(agemum_cat_)

*income
gen inc = exp(inc_ln)
gen hhincome_cat=.
replace hhincome_cat=1 if inc<50
replace hhincome_cat=2 if inc>=50 & inc<100
replace hhincome_cat=3 if inc>=100 & inc!=.
tab hhincome_cat, gen(hhincome_cat_)

*years of education
gen educd_cat=.
replace educd_cat=1 if eduyears<13
replace educd_cat=2 if eduyears>=13 & eduyears<16
replace educd_cat=3 if eduyears>=16 & eduyears!=.
tab educd_cat, gen(educresp_cat_)


* generate Weights
local vars 		gender_cat_* ///
				educmother_cat_* educfather_cat_* ///
				agemum_cat_* ///
				/*hhincome_cat_**/
preserve
postfile weight ///
		 str20 var total ///
		 using "${temp}reweight.dta", replace

foreach x of varlist `vars' {
			post weight ("`x'") ///
			(`=``x'_white'*`total'') //Records relevant population size
		}
postclose weight
use "${temp}reweight.dta", clear
mkmat total, matrix(t)
restore
sreweight `vars', sw(w0) nw(w1) tot(t) df(chi2)
replace w1=0 if w1<0

*save
keep 	gender_cat_* ///
		educresp_cat_* ///
		educmother_cat_* ///
		educfather_cat_* ///
		agemum_cat_*  ///
		hhincome_cat_*  ///
		aid w0 w1
foreach x of varlist _all{
	sum `x' [aw=w0], meanonly
	local `x'_sample=r(mean)

	sum `x' [aw=w1], meanonly
	local `x'_weighted=r(mean)
}


// ---------------------------------------
// Consolidate Representativity Comparison
// ---------------------------------------
postfile represent ///
		 str20 var pop white sample weighted ///
		 using "${temp}represent.dta", replace

foreach x of varlist ///
		gender_cat_* ///
		educmother_cat_* ///
		educfather_cat_* ///
		agemum_cat_*  ///
		hhincome_cat_* ///
		educresp_cat_*{
			post represent ("`x'") ///
			(``x'_pop') (``x'_white') ///
			(``x'_sample') (``x'_weighted') ///

		}
postclose represent


// -----------------
// Issue Weight-File
// -----------------
keep aid w0 w1
save "${temp}reweight.dta", replace
